package com.ss.android.article.base.feature.favorite;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ViewGroup;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.main.IRecentFragment;
import com.ss.android.common.app.AbsPagerAdapter;
import com.ss.android.common.app.UIScreenContext;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import inline.DrawerLayoutDemo;

public final class CateAdapter extends AbsPagerAdapter {
    private WeakReference a;
    private int b;
    private Field c;
    private DrawerLayoutDemo.b g;
    private ViewPager h;
    private Context i;

    public CateAdapter(FragmentManager arg5, ViewPager arg6, DrawerLayoutDemo.b arg7) {
        super(arg5);
        this.b = -1;
        this.c = null;
        new HashMap();
        AppData.getInstance();
        this.h = arg6;
        this.i = arg6.getContext();
        this.g = arg7;
        try {
            this.c = AbsPagerAdapter.class.getDeclaredField("mFragmentTransaction");
            this.c.setAccessible(true);
        } catch (Exception v0) {
            Logger.w("CateAdapter", "get mCurTransaction Field exception: " + v0);
        }
    }

    public final int getCount() {
        int v0 = AppData.getInstance().getAbSettings().isFeedClearAllRefreshEnable() ? 4 : 3;
        return v0;
    }

    public final int getItemPosition(Object arg6) {
        int v1;
        int v2 = -2;
//        if((arg6 instanceof e)) {
//            String v3 = ((e)arg6).getCategory();
//            if(!android.support.design.a.f(v3)) {
//                Iterator v4 = null.iterator();
//                v1 = 0;
//                while(true) {
//                    if(!v4.hasNext()) {
//                        return v2;
//                    }
//                    else if(!v3.equals(v4.next().d)) {
//                        ++v1;
//                        continue;
//                    }
//
//                    return v1;
//                }
//            }
//            else {
//                return v2;
//            }
//        } else {
//            v1 = v2;
//        }

        return v2;
    }

    public final CharSequence getPageTitle(int arg3) {
        String v0;
        switch (arg3) {
            case 0: {
                v0 = this.i.getString(com.ss.android.article.browser.R.string.title_favorite);
                break;
            }
            case 1: {
                v0 = this.i.getString(com.ss.android.article.browser.R.string.eg);
                break;
            }
            case 2: {
                v0 = this.i.getString(com.ss.android.article.browser.R.string.t0);
                break;
            }
            case 3: {
                if (this.getCount() <= 3) {
                    return "";
                }

                v0 = this.i.getString(com.ss.android.article.browser.R.string.td);
                break;
            }
            default: {
                label_1:
                v0 = "";
                break;
            }
        }

        return ((CharSequence) v0);
    }

    public final void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        if ((object instanceof Fragment)) {
            try {
                if (this.mFragmentTransaction == null) {
                    return;
                }

                this.mFragmentTransaction.remove(((Fragment) object));
            } catch (Exception v0) {
                Logger.w("CateAdapter", "destroyItem remove fragment exception: " + v0);
            }
        }
    }

    public final Fragment createFragment(int position) {
        switch (position) {
            case 0:
                return new FavoriteArticleListFragment();
            case 1:
                return new ReadHistoryArticleListFragment();
            case 2: {
                return new PushHistoryListFragment();
            }
            case 3:
                if (this.getCount() > 3) {
                    r v0_3 = new r();
                    Bundle bundle = new Bundle();
                    bundle.putString("category", "feed_refresh_history");
                    bundle.putString("category_id", "");
                    v0_3.setArguments(bundle);
                    return v0_3;
                }
        }
        throw new IllegalArgumentException();
    }

    public final void setPrimaryItem(ViewGroup container, int position, Object object) {
        Object v1 = null;
        int v4 = 2;
        if (this.b != position) {
            Logger.v("CateAdapter", "setPrimaryItem " + position);
        }

        this.b = position;
        if (object != null) {
            if (this.a != null) {
                v1 = this.a.get();
            }

            if (v1 != object && ((v1 instanceof IRecentFragment))) {
                ((IRecentFragment) v1).onUnsetAsPrimaryPage(v4);
            }
            if (v1 == object) {
            } else {
                this.a = new WeakReference(object);
                if (!(object instanceof IRecentFragment)) {
                } else {
                    ((IRecentFragment) object).onSetAsPrimaryPage(v4);
                }
            }
        } else {
            this.a = ((WeakReference) v1);
        }

        Object v0 = object;
        if (v0 != this.mFragment && ((v0 instanceof UIScreenContext)) && this.g != null) {
            HashMap v1_1 = new HashMap();
            ((Map) v1_1).clear();
            ((UIScreenContext) v0).setEnterContext(((Map) v1_1));
        }

        super.setPrimaryItem(container, position, object);
    }

    public final Fragment d() {
        return this.mFragmentManager.findFragmentByTag(this.getTag(this.h.getId(), 0));
    }
}

